package com.example.shouybl.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.GiftMobileAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.GiftData;
import com.example.shouybl.constant.Path;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class GameGiftFragment extends Fragment {

    private String id = "";

    private android.widget.ListView lvgamegift;
    private ArrayList<GiftData> giftDatas=new ArrayList<>();
    private AsyncHttpClient client = new AsyncHttpClient();
    private GiftMobileAdapter gameGiftAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_game_gift, container, false);

        initView(view);
        initData();
        return view;
    }

    private void initData() {
        client.post(Path.PATH_GAME_GIFT, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<GiftData>> typeReference=new TypeReference<AppBaseBean<GiftData>>(){};
                AppBaseBean baseBean= JSONObject.parseObject(responseString,typeReference);
                if (baseBean!=null && baseBean.getState().equals("success")){
                    giftDatas.addAll(baseBean.getInfo());
                }
                gameGiftAdapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams getParams() {
        RequestParams params=new RequestParams();
        params.put("id",id);
        params.put("compare","02746f27bbc4108a98605b192ea140ea");
        return  params;
    }

    private void initView(View view) {
        id = getArguments().getString("id");
        this.lvgamegift = (ListView) view.findViewById(R.id.lv_game_gift);
        gameGiftAdapter=new GiftMobileAdapter(giftDatas, BaseApp.getInstance());
        lvgamegift.setAdapter(gameGiftAdapter);
    }


}
